package com.breaktian.assemble.livedatabus;

import java.util.HashMap;
import java.util.Map;


public final class LiveDataBus {

    final Map<String, BusLiveData<Object>> bus;

    private LiveDataBus() {
        bus = new HashMap<>();
    }

    private static class SingletonHolder {
        private static final LiveDataBus DEFAULT_BUS = new LiveDataBus();
    }

    public static LiveDataBus getDefault() {
        return SingletonHolder.DEFAULT_BUS;
    }


    public synchronized <T> ISubscribe<T> channel(String key, Class<T> type) {
        if (!bus.containsKey(key)) {
            bus.put(key, new BusLiveData<>(key));
        }
        return (ISubscribe<T>) bus.get(key);
    }

    public ISubscribe<Object> channel(String key) {
        return channel(key, Object.class);
    }


}
